package autoFile.main;

import autoFile.annotation.AutoFile;
import autoFile.core.strategy.interfaces.CreateFileStrategyInterface;
import autoFile.enums.CreateDbsFileEnum;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-08-16
 * Time: 下午5:20
 * Description: ${description}
 */
public class DbsAutoApp {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IOException {
        doMain();
    }

    public static void doMain() throws ClassNotFoundException, IOException, IllegalAccessException, InstantiationException {
        CreateFileStrategyInterface createFileStrategy;

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[]{"classpath*:dbs/application-context-dbs*.xml"});
        Map<String, Object> beans = ctx.getBeansWithAnnotation(AutoFile.class);
        deleteFile();
        for (Object o : beans.values()) {
            String className = o.getClass().getName();
            int $Index = o.getClass().getName().indexOf("$");
            Class clazz = Class.forName(className.substring(0, $Index));
            for (CreateDbsFileEnum createFileEnum : CreateDbsFileEnum.values()) {
                createFileStrategy = (CreateFileStrategyInterface) createFileEnum.getValue().newInstance();
                createFileStrategy.createFile(clazz);

            }
        }
    }

    private static void deleteFile() {
        File file = new File("/Users/liuliwei/autoFile");
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (!file1.isDirectory()) {
                System.out.println("delete file" + file1.getName());
                file1.delete();
            }
        }
        System.out.println("######################");
    }
}
